<%@page pageEncoding="UTF-8" isELIgnored="false" session="false"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@taglib prefix="f" uri="http://www.slim3.org/functions"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<%@page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@page import="sandbox.util.AccountUtils"%>
<%@page import="com.google.appengine.api.users.UserService"%>
<!DOCTYPE html>
<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>index</title>
	<link rel="stylesheet" type="text/css" href="/css/global.css" />
</head>

<body>
	<article>
	<h1>ranking</h1>
	<c:if test="${ranking != null}">
		<% int rank = 1; %>
		<table border="1">
			<c:forEach var="counter" items="${ranking}">
				<tr>
					<th align="center"><%= rank %>位</th>
					<td><a href="${f:url('/redirect')}?key=${counter.key}">${counter.title}</a></td>
					<td align="right">${counter.counter}</td>
					<% rank++; %>
				</tr>
			</c:forEach>
		</table>
	</c:if>
	</article>

	<article>
		<div align="right">
			<%
				 UserService userService = UserServiceFactory.getUserService();
				boolean isAdmin = AccountUtils.isAdmin(request);
			    if (isAdmin) {
			%> <a href="<%= userService.createLogoutURL("/index") %>">logout</a> <% } else { %>
			<a href="<%= userService.createLoginURL("/index") %>">login</a> <% } %>
		</div>

		<h1>ERO SAMPLE COLLECTOR</h1>

		<c:if test="${siteList != null}">
			<a href="${f:url('/index')}">all</a>
			<c:forEach var="site" items="${siteList}">
				|<a href="${f:url('/index')}?key=${f:key(site.key)}">${site.title}</a>
			</c:forEach>
		</c:if>

		<c:if test="${sampleMap != null}">
			<c:forEach var="entry" items="${sampleMap}">
				<c:if test="${entry.value != null}">
					<h1 style="margin-bottom: 0px;"><fmt:formatDate value="${entry.key}" pattern="yyyy/MM/dd" /></h1>
					<% int counter = 1; %>
					<table border="1" width="900">
						<c:forEach var="sample" items="${entry.value}">
							<% if (counter % 3 == 1) { %>
								</tr>
								<tr>
							<% } %>
							<td width="300" style="padding: 10px;">
								<h2 style="margin: 0px;"><c:out value="${sample.title}" /></h2>
								<c:if test="${sample.image != null}">
									<center><a href="${f:url('/redirect')}?key=${f:key(sample.key)}" target="_blank"> <img src="${sample.image}" border="0" style="margin: 10px; max-width: 280px; max-height: 280px;" /> </a></center>
								</c:if><a href="${f:url('/index')}?key=${f:key(sample.siteKey)}">${sample.siteTitle}</a><br />
								<div align="right">${sample.timestampString}<br />
									<% if (isAdmin) { %>
										[<a href="${f:url('/admin/delete')}?key=${f:key(sample.key)}">del</a>]
									<% } %>
								</div>
							</td>
							<% counter++; %>
						</c:forEach>
					</c:if>
				</table>
			</c:forEach>
		</c:if>
		<section>

		</section>
	</article>
</body>
</html>
